package com.sunday.buildpattern.entity;

/**
  @program: design-patterns
  @author: Jh
  @create: 2021-09-06 14:46
*/
public class UserEntity {

    private Long id;
    private String name;
    private Integer sex;

    private UserEntity() {

    }

    public UserEntity(UserBuilder userBuilder) {
        this.id = userBuilder.id;
        this.name = userBuilder.name;
        this.sex = userBuilder.sex;
    }

    public static UserEntity.UserBuilder builder() {
        return new UserBuilder();
    }

    public static class UserBuilder {
        private Long id;
        private String name;
        private Integer sex;

        public UserEntity build() {
        /*

            在创建对象之前还可以对参数做校验

        if (id == 1) {
                throw new IllegalArgumentException("id not in one");
            }

            if ("JiangHai".equals(name)){
                throw new IllegalArgumentException("name not call JiangHai");
            }*/
            return new UserEntity(this);
        }

        public UserBuilder id(Long id) {
            this.id = id;
            return this;
        }

        public UserBuilder name(String name) {
            this.name = name;
            return this;
        }

        public UserBuilder sex(Integer sex) {
            this.sex = sex;
            return this;
        }
    }
}
